/**
 * @fileOverview Thesaurus for RosNano.
 * Button name : Thesaurus.
 */

(function() {

   // Add thesaurus plugin.
   VED.plugins.add( 'thesaurus', {
      requires: 'dialog',
      lang: 'ru,en', // %REMOVE_LINE_CORE%
      icons: 'thesaurus', // %REMOVE_LINE_CORE%
      hidpi: true, // %REMOVE_LINE_CORE%


      init: function( editor ) {
         if ( editor.blockless )
            return;

         var self = this;

         // Delete span[data-thesaurusid] when text pasting in editor (#6921)
         var dataFilter = editor.dataProcessor && editor.dataProcessor.dataFilter;
         var dataFilterRules = {
            elements: {
               span: function( element ) {
                  var attrs = element.attributes;
                  if ( attrs && attrs[ 'data-thesaurusid' ] )
                     delete element.name;
               }
            }
         };
         dataFilter && dataFilter.addRules( dataFilterRules );

         // Add the thesaurus button.
         editor.addCommand( 'thesaurus', new VED.dialogCommand( 'thesaurus' ) );

         editor.ui.addButton && editor.ui.addButton( 'Thesaurus', {
            label: editor.lang.thesaurus.toolbar,
            command: 'thesaurus',
            toolbar: 'spellchecker,40'
         });

         // Register the dialog.
         VED.dialog.add( 'thesaurus', this.path + 'dialogs/thesaurus.js' );

         editor.on( 'doubleclick', function( evt ) {
            var element = VED.plugins.thesaurus.getSelectedLink( editor ) || evt.data.element,
                sourceRuRegex = new RegExp( '^' + editor.config.thesaurusSourcePatternRu.replace( '{term_id}', '(\\d+)' ).replace(/\./g, '\\.') + '$' ),
                sourceEnRegex = new RegExp( '^' + editor.config.thesaurusSourcePatternEn.replace( '{term_id}', '(\\d+)' ).replace(/\./g, '\\.') + '$' );

            if ( !element.isReadOnly() ) {
               var href = element.getAttribute( 'href' );
               if ( element.is( 'a' ) && href && ( sourceRuRegex.test(href) || sourceEnRegex.test(href) ) ) {
                  evt.data.dialog = 'thesaurus';
                  editor.getSelection().selectElement( element );
               }
            }
         }, null, 9);

         // If the "menu" plugin is loaded, register the menu items.
         if ( editor.addMenuItems ) {
            editor.addMenuItems({
               thesaurus: {
                  label: editor.lang.thesaurus.menu,
                  command: 'thesaurus',
                  group: 'link',
                  order: 10
               }
            });
         }

         // If the "contextmenu" plugin is loaded, register the listeners.
         if ( editor.contextMenu ) {
            editor.contextMenu.addListener( function( element, selection ) {
               if ( !element || element.isReadOnly() )
                  return null;

               var sourceRuRegex = new RegExp( "^" + editor.config.thesaurusSourcePatternRu.replace( /\{term_id\}/, '(\\d+)' ).replace(/\./g, '\\.') + "$" ),
                   sourceEnRegex = new RegExp( "^" + editor.config.thesaurusSourcePatternEn.replace( /\{term_id\}/, '(\\d+)' ).replace(/\./g, '\\.') + "$" );

               var anchor = VED.plugins.link.getSelectedLink( editor );
               if ( !anchor ) return null;

               var menu = {};

               var href = anchor.getAttribute( 'href' )
               if ( href && ( sourceRuRegex.test(href) || sourceEnRegex.test(href) ) )
                  menu = { thesaurus: VED.TRISTATE_OFF };

               return menu;
            });
         }

      }

   });

   VED.plugins.thesaurus = {

      getSelectedLink: function( editor ) {
         var selection = editor.getSelection();
         var selectedElement = selection.getSelectedElement();
         if ( selectedElement && selectedElement.is( 'a' ) )
            return selectedElement;

         var range = selection.getRanges( true )[ 0 ];

         if ( range ) {
            range.shrink( VED.SHRINK_TEXT );
            return editor.elementPath( range.getCommonAncestor() ).contains( 'a', 1 );
         }
         return null;
      }

   };

   VED.config.thesaurusSourcePatternRu = "http://thesaurus.rusnano.com/wiki/article{term_id}";
   VED.config.thesaurusSourcePatternEn = "http://eng.thesaurus.rusnano.com/wiki/article{term_id}";
   VED.config.thesaurusTagClass = "nano-term";

})();



